var windowName = "siteLog";
var Page,load=0
var files = new List()

apiready = function(){
    var CarPics = new PicUp()
    onWindowShow(function () {
        var cur = getLocData("curSite")
        Page.curSite = cur;
        getData(Page)
        files.setData({id:cur.id})
        if (load >0) {
            files.reload()
        }
        load++
    })
    onWindowHide(function () {
    })
    console.log("index page is OK");

    var imageBrowser = api.require('imageBrowser');
    Page = createVM("#Page",{
        //页面数据
        tab:0,
        curSite:{},
        sites:[],
        list:[],
        listPic:[],
        pics:[],
        en:{
            "id": "",
            "site": "",
            "intro": "",
            "content": ""
        },
        atta:[],//展示层图片
        open:false,//展示层是否打开
    },{
        //页面方法
        "init":function () {
            var _this = this;
            startPullDownRefresh();
            var cur = getLocData("curSite")
            files.setData({id:cur.id})
            files.setUrl("/app/site/log")
                .setCbFunc(function (data) {
                    stopPullDownRefresh();
                    stopPullUpLoadMore();
                    _this.list = data
                    var pics = []
                    data.map(function (item) {
                        pics.push(item.FileLog_file)
                    })
                    _this.listPic = pics
                }).getList()
        },
        OnPullDownRefresh:function () {
            startPullDownRefresh();
            files.reload()
        },
        OnPullUpLoadMore:function () {
            if (this.tab === 0) {
                startPullUpLoadMore();
                files.getList()
            }
        },
        submit:function() {
            var _this = this
            this.$refs.form.validate().then((result) => {
                if (result){
                    _this.en.site = _this.curSite.id
                    var form = createForm(_this.en);
                    apiPost("/app/site/log/add",form,function (res) {
                        // log(res)
                        files.reload()
                        _this.tab = 0
                        showToast(res.message)
                    })

                }else{
                    showToast("请检查输入")
                }
            });
        },
        selectPic:function () {
            var _this = this;
            CarPics.select(9).setCbFunc(function (pics) {
                _this.pics = CarPics.getViewData(pics)
                _this.en.content = pics
            })
        },
        del:function (index) {
            var pics = CarPics.removePic(index)
            this.pics = CarPics.getViewData(pics)
        },
        move:function (index,num) {
            var pics = CarPics.picMove(index,num)
            this.pics = CarPics.getViewData(pics)
        },
        showPic:function (index,pics) {
            imageBrowser.openImages({
                imageUrls: pics,
                activeIndex:index
            });
        }
    })

    Page.init()

}

function getData(_this) {
    apiGet("/app/site/log/add",{},function (res) {
        _this.en = res.data.en
    })
}